#define MAX_SIZE 1000;

class Stack
{
    public:
        Stack::Stack(): _top(0) {init_lock(&_lock);};
        Stack::~Stack(): {destroy_lock(&_lock);};
        void Push(int i);
        bool CanPop();
        int Pop();
    private:
        mutex_lock _lock;
        int _top;
        // stack that can accommodate 0-MAX_SIZE-1
        int _s[MAX_SIZE];
};

void Stack::Push(int i) {
    mutex_lock(&lock);
    if (_top >= MAX_SIZE) {
        // overflow
        return;
    }
    _s[_top++] = i;
    mutex_unlock(&lock);
}

bool Stack::CanPop() {
    int res;
    mutex_lock(&lock);
    res = (_top > 0)?true:false;
    mutex_unlock(&lock);
    return res;
}

int Stack::Pop() {
    if (CanPop()) {
        mutex_lock(&lock);
        int res = _s[--_top];
        mutex_unlock(&lock);
        return res;
    }
    // assume errint is the errno
    return errint;
}
